﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Device_Finder_MVC4.Models;
using System.Security.Cryptography;
using System.Text;

namespace Device_Finder_MVC4.DAL
{
    public class TrackLogDAL : BaseDAL
    {
        public bool InsertLocationToID(int IDDevice, float latitude, float longtitude, string IPAddress, DateTime TimeLog)
        {

            TRACKLOG trackLog = new TRACKLOG();
            trackLog.IDDEVICE = IDDevice;
            trackLog.LATITUDE = latitude;
            trackLog.LONGTITUDE = longtitude;
            trackLog.IPADDRESS = IPAddress;
            trackLog.TIMELOG = TimeLog;

            this.entities.TRACKLOGs.Add(trackLog);

            if (this.entities.SaveChanges() > 0)
                return true;
            return false;
        }

        public IQueryable<TRACKLOG> GetTrackLogByID(int IDDevice)
        {
            var lstTrackLog = (from devices in entities.TRACKLOGs
                               where devices.IDDEVICE == IDDevice
                               orderby devices.TIMELOG descending
                               select devices).Take(100);

            return lstTrackLog;
        }

        public bool Delete(int IDDevice)
        {

            var lstTracklog = GetTrackLogByID(IDDevice);
            
            if (lstTracklog.Count() == 0)
                return true;

            foreach (var tracklog in lstTracklog)
            {
                entities.TRACKLOGs.Remove(tracklog);
            }

            if (entities.SaveChanges() > 0)
                return true;

            return false;
        }

    }
}